package com.mtwp.dfs.service.category.stateprocess.child;

import com.alibaba.fastjson.JSON;
import com.mtwp.dfs.domain.category.DiskStore;
import com.mtwp.dfs.repository.DiskStoreRepository;
import com.twp.auth.domain.authority.user.ManageUser;
import com.twp.auth.repository.user.ManageUserRepository;
import com.twp.common.component.process.statemachine.annotation.IMapFunction;
import com.twp.common.component.process.statemachine.annotation.MapFunctionListener;
import com.twp.common.component.process.statemachine.domain.StateNode;
import com.twp.common.tuple.RequestDto;
import com.twp.common.tuple.ResultDTO;
import lombok.Builder;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.Principal;

/*
检查权限的子状态机
 */
@Service
@Slf4j
@Builder
public class AuthFuncMap implements IMapFunction {

    @Autowired
    DiskStoreRepository diskStoreRepository;

    @Autowired
    ManageUserRepository manageUserRepository;

    /*
    状态入口：存储操作请求检查
     */
    @MapFunctionListener(name = "存储操作权限检查处理参数",group = "dfs_auth",code = "start")
    public StateNode getNodeData(StateNode<DiskStore, ResultDTO> currentNode){
        val requestDto = currentNode.getRequestDto();
        Principal principal = requestDto.getPrincipal();
        DiskStore diskStore = requestDto.getData();
        log.info("当前用户:",principal.getName());
        log.info("当前操作的存储对象:", JSON.toJSONString(diskStore));
        if (diskStore.getId()!=null){
            diskStore = diskStoreRepository.findOne(diskStore.getId());
        }else {
            ManageUser manageUser = manageUserRepository.findAllByUserNameAndStatus(principal.getName(),1);
            if (manageUser!=null){
                diskStore.setManageUser(manageUser);
            }
        }
        DiskStore finalDiskStore=diskStore;
        //进入节点检查
        return StateNode.builder().nodeCode("dfs_auth_check").requestDto(RequestDto.builder().principal(principal).data(finalDiskStore).build()).build();
    }

    @MapFunctionListener(name = "存储操作权限检查开始",group = "dfs_auth",code = "check")
    public StateNode check(StateNode<DiskStore, ResultDTO> currentNode){
        log.info("存储操作权限检查开始");
        val requestDto = currentNode.getRequestDto();
        Principal principal = requestDto.getPrincipal();
        DiskStore diskStore = requestDto.getData();

        ResultDTO resultDTO = ResultDTO.builder().build();
        //目前只支持个人操作各自的存储节点，后续增加其他的华，即在此分离检查
        if ((diskStore.getManageUser()!=null)&&principal.getName().equals(diskStore.getManageUser().getUserName())){
            if (diskStore.getStatus()!=null&&diskStore.getStatus().equals(0)){
                resultDTO.setIsSuccess(false);
                resultDTO.setMessage("非法操作已锁定的权限存储节点");
            }
        }else {
            resultDTO.setIsSuccess(false);
            resultDTO.setMessage("当前操作人非法操作权限存储");
        }
        return StateNode.builder().nodeCode("dfs_auth_end").response(
                resultDTO
        ).build();
    }


    @MapFunctionListener(name = "存储操作权限检查结束",group = "dfs_auth",code = "end")
    public StateNode dealEnd(StateNode currentNode){
        log.info("dfs_auth_end");
        return StateNode.builder().response(currentNode.getResponse()).build();
    }
}
